﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Global;
using Microsoft.Win32;

namespace EnglishReminder
{
    public partial class SettingsForm : Form
    {
        //Delegate function
        public delegate void RestartReminderForm();
        //delegate object
        public RestartReminderForm restartReminderForm;

        public SettingsForm()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {
            tbDisplayTime.Text = Settings.DISPLAY_TIME.ToString();
            tbWaitingTime.Text = Settings.WAITING_TIME.ToString();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!Validator.IsValidNumber(tbDisplayTime.Text))
            {
                MessageBox.Show("The display time is invalid. Remember: \n\n"
                                +"  + Please use integer except float.\n"
                                +"  + The number has to be > 0 and not too big.\n"
                                +"  + Don't use character, just use number.");
                return;
            }
            if (!Validator.IsValidNumber(tbWaitingTime.Text))
            {
                MessageBox.Show("The waiting time is invalid. Remember: \n\n"
                                + "  + Please use integer except float.\n"
                                + "  + The number has to be > 0 and not too big.\n"
                                + "  + Don't use character, just use number.");
                return;
            }

            //save the values to the settings
            Settings.DISPLAY_TIME = Int32.Parse(tbDisplayTime.Text);
            Settings.WAITING_TIME = Int32.Parse(tbWaitingTime.Text);

            

            //save the same values to the registry
            var regMySoft = Utility.CreateRegistryKey();
            if (regMySoft != null)
            {
                using (var tempKey = regMySoft.OpenSubKey(Constant.REGISTRY.SETTINGS, true))
                {
                    if (tempKey != null)
                    {
                        tempKey.SetValue(Constant.REGISTRY.DISPLAY_TIME, Settings.DISPLAY_TIME.ToString());
                        tempKey.SetValue(Constant.REGISTRY.WAITING_TIME, Settings.WAITING_TIME.ToString());
                        tempKey.Close();
                    }
                }
            }

            //restart the Reminder form
            if (restartReminderForm != null)
                restartReminderForm();

            this.Close();
        }
    }
}
